package com.xca.znb.presenter;

import com.xca.znb.contact.GoodsDetailContact;
import com.xca.znb.entity.Collect;
import com.xca.znb.entity.Goods;
import com.xca.znb.entity.Local;
import com.xca.znb.entity.NewsLetter;
import com.xca.znb.entity.User;
import com.xca.znb.model.imodel.IGoodsDetail;

public class GoodsDetailsPresenter {
    private GoodsDetailContact goodsDetailContact ;
    private IGoodsDetail iGoodsDetail ;
    public GoodsDetailsPresenter(GoodsDetailContact contact , IGoodsDetail iGoodsDetail){
        this.goodsDetailContact = contact ;
        this.iGoodsDetail = iGoodsDetail ;
    }

    public void goodsDetails(int id){
        Goods goods =iGoodsDetail.getGoodsById(id);
        if (goods != null){
            String phone = goods.getPublisher() ;
            User user = iGoodsDetail.getUserByPhone(phone);
            String imgTitle = goods.getFirst_image();
            double price = goods.getPrice();
            String unit = goods.getUnit();
            int gId = goods.getId();
            String goodsId = String.valueOf(gId);
            String goodsName = goods.getName() ;
            String address = goods.getAddress() ;
            String freight = goods.getFreight() ;
            String userConcern = user.getConcern() ;
            String userName = user.getName() ;
            String goodsText = goods.getText();
            String imgList = goods.getImg_list() ;
            if (userName == null){
                goodsDetailContact.showGoodsMessage(imgTitle , price , unit , goodsId , goods.getPublisher() , address ,
                        freight , userConcern , phone , goodsText , imgList);
            }else {
                goodsDetailContact.showGoodsMessage(imgTitle , price , unit , goodsId , goodsName , address ,
                        freight , userConcern , userName , goodsText , imgList);
            }
        }
    }
    public Goods getGoodsById(int id){
        return iGoodsDetail.getGoodsById(id);
    }

    public void addNewsletter(String phone1 , String phone2 , String time){
        NewsLetter newsLetter = iGoodsDetail.getNewsLetterByPhone(phone1, phone2);
        NewsLetter newsLetter1 = iGoodsDetail.getNewsLetterByPhone(phone2 , phone1);
        if (newsLetter == null && newsLetter1 == null){
            iGoodsDetail.addNewsletter(phone1, phone2, time);
        }
    }
    public int getLocalDefaultId(String phone){
        return iGoodsDetail.getLocalDefaultId(phone);
    }



    public void addCollect(String gId, String type, String user) {
        iGoodsDetail.addCollect(gId, type, user);
    }

    public void delCollect(int id) {
        iGoodsDetail.delCollect(id);
    }


    public Collect getCollect(String gId, String type, String user) {
        return iGoodsDetail.getCollect(gId,type,user);
    }

    public void addCart(int goodsId , int num , String user){
        iGoodsDetail.addCart(goodsId , num , user);
    }
}
